<template>
  <div class="app-container">
    <el-form
      size="small"
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="用户名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="授权人姓名" prop="authorizer">
        <el-input
          v-model="queryParams.authorizer"
          placeholder="请输入授权人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="企业名称" prop="enterpriseName">
        <el-input
          v-model="queryParams.enterpriseName"
          placeholder="请输入企业名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择审核状态">
          <el-option
            v-for="item in authState"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8" >
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['internet:intUser:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['internet:intUser:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['internet:intUser:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['internet:intUser:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row> -->

    <el-table
      v-loading="loading"
      :data="intUserList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="授权人姓名" align="center" prop="authorizer" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column
        label="企业名称"
        align="center"
        show-overflow-tooltip
        prop="enterpriseName"
      />
      <el-table-column label="企业类型" align="center" prop="enterpriseType" />
      <el-table-column
        label="法定代表人"
        align="center"
        prop="legalRepresentative"
      />
      <el-table-column label="审核状态" align="center" prop="stateNew" />
      <el-table-column label="用户类型" align="center" prop="typeNew" />
      <el-table-column
        label="操作"
        align="center"
        width="200"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['internet:intUser:edit']"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.stateNew === '审核中'"
            @click="handle(scope.row)"
            v-hasPermi="['internet:intUser:edit']"
            >审核</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改互联网用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form
        size="small"
        ref="form"
        :model="form"
        :rules="rules"
        label-width="90px"
      >
        <el-form-item label="用户名" prop="userName">
          <el-input  :disabled="delFlag" v-model="form.userName" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="授权人姓名" prop="authorizer">
          <el-input  :disabled="delFlag" v-model="form.authorizer" placeholder="请输入授权人姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input  :disabled="delFlag" v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input  :disabled="delFlag" v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input  :disabled="delFlag" v-model="form.idCard" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="企业名称" prop="enterpriseName">
          <el-input
           :disabled="delFlag"
            v-model="form.enterpriseName"
            placeholder="请输入企业名称"
          />
        </el-form-item>
        <el-form-item   label="企业类型" prop="enterpriseType">
          <el-select :disabled="delFlag" v-model="form.type" placeholder="请选择用户类型">
            <el-option
              v-for="item in enterpriseType"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="营业执照号码" prop="businessLicense">
          <el-input
           :disabled="delFlag"
            v-model="form.businessLicense"
            placeholder="请输入营业执照号码"
          />
        </el-form-item>
        <el-form-item label="统一社会信用代码" prop="creditCode">
          <el-input
           :disabled="delFlag"
            v-model="form.creditCode"
            placeholder="请输入统一社会信用代码"
          />
        </el-form-item>
        <el-form-item label="法定代表人" prop="legalRepresentative">
          <el-input
           :disabled="delFlag"
            v-model="form.legalRepresentative"
            placeholder="请输入法定代表人"
          />
        </el-form-item>
        <el-form-item label="省" prop="province">
          <el-input   :disabled="delFlag" v-model="form.province" placeholder="请输入省" />
        </el-form-item>
        <el-form-item label="市" prop="city">
          <el-input  :disabled="delFlag" v-model="form.city" placeholder="请输入市" />
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input  :disabled="delFlag" v-model="form.address" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="邮箱地址" prop="email">
          <el-input  :disabled="delFlag" v-model="form.email" placeholder="请输入邮箱地址" />
        </el-form-item>
        <el-form-item label="审核状态" prop="state">
          <el-select  :disabled="delFlag" v-model="form.state" placeholder="请选择审核状态">
            <el-option
              v-for="item in authState"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户类型" prop="type">
          <el-select  :disabled="delFlag" v-model="form.type" placeholder="请选择用户类型">
            <el-option
              v-for="item in userType"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否通过审核" v-if='isHandle'>
          <el-radio-group v-model="examine" >
            <el-radio label="0">是</el-radio>
            <el-radio label="1">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="未通过审核原因" prop="reason" v-if='isHandle&&examine==="1"'>
          <el-input v-model="form.reason" placeholder="请输入未通过审核原因" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if='isHandle'>确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listIntUser,
  getIntUser,
  delIntUser,
  addIntUser,
  updateIntUser,
} from "@/api/indusInternet/internet";
import { listData } from "@/api/system/dict/data";

export default {
  name: "IntUser",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 互联网用户表格数据
      intUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        authorizer: null,
        phone: null,
        password: null,
        idCard: null,
        enterpriseName: null,
        enterpriseType: null,
        businessLicense: null,
        creditCode: null,
        legalRepresentative: null,
        province: null,
        city: null,
        address: null,
        email: null,
        state: null,
        type: null,
        reason: null,
      },
      
      authState: [], //审核状态
      enterpriseType: [], //企业类型
      userType: [],
      // 表单参数
      form: {},
      examine: "0",
      delFlag: false,
      isHandle: false, //审核
      // 表单校验
      rules: {
        reason: [
          {
            required: true,
            message: "请输入未通过原因",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    //企业类型
    listData({ dictType: "enterprise_type" }).then((response) => {
      this.enterpriseType = response.rows;
    });
  },
  methods: {
    /** 查询互联网用户列表 */
    getdictDataList() {
      listData({ dictType: "auth_state" }).then((response) => {
        this.authState = response.rows;
      });
      //用户类型
      listData({ dictType: "internet_user_type" }).then((response) => {
        this.userType = response.rows;
      });
    },
    async getList() {
      this.loading = true;
      await this.getdictDataList();
      listIntUser(this.queryParams).then((response) => {
        response.rows.forEach((item) => {
          this.authState.forEach((itemType) => {
            if (itemType.dictValue === item.state) {
              item.stateNew = itemType.dictLabel;
            }
          });
          this.userType.forEach((itemType) => {
            if (itemType.dictValue === item.type) {
              item.typeNew = itemType.dictLabel;
            }
          });
        });
        this.intUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userName: null,
        authorizer: null,
        phone: null,
        password: null,
        idCard: null,
        enterpriseName: null,
        enterpriseType: null,
        businessLicense: null,
        creditCode: null,
        legalRepresentative: null,
        province: null,
        city: null,
        address: null,
        email: null,
        state: null,
        type: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    // handleAdd() {
    //   this.reset();
    //   this.open = true;
    //   this.delFlag = false;
    //   this.isHandle = false;
    //   this.title = "添加互联网用户";
    // },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      this.delFlag = true;
        this.isHandle = false;
      getIntUser(id).then((response) => {
        this.form = response.data;
        this.$set(this.form, "reason", "");
        this.open = true;
        this.title = "互联网用户详情";
      });
    },
    handle(row) {
      this.reset();
      const id = row.id || this.ids;
      this.delFlag = true;
      this.isHandle = true;
      getIntUser(id).then((response) => {
        this.form = response.data;
        this.$set(this.form, "reason", "");
        this.open = true;
        this.title = "互联网用户审核";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.examine === "0") {
            this.form.state = "2";
          } else {
            this.form.state = "3";
          }
          updateIntUser(this.form).then((response) => {
            this.msgSuccess("发送成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除互联网用户编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delIntUser(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "internet/intUser/export",
        {
          ...this.queryParams,
        },
        `internet_intUser.xlsx`
      );
    },
  },
};
</script>